package tp;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import facade.FacadeInitialisation;

import tp.model.Client;
import tp.model.Train;
import tp.model.Trajet;
import tp.model.Ville;
import tp.util.DataSingleton;
import tp.util.DateTool;

/**
 * InsertData class for tp. This creates a simple product catalog, creates a
 * customer and order and amends the order.
 * 
 */
public class InsertData {

	public static void main(String[] args) {
		try {
			// load versant.properties project file as properties to connect
			Properties p = loadProperties();

			// run against local or remote Versant JDO Interface server
			PersistenceManagerFactory pmf = JDOHelper
					.getPersistenceManagerFactory(p);
			InsertData main = new InsertData(pmf.getPersistenceManager());
			try {
				main.go();
			} finally {
				main.pm.close();
			}
			System.exit(0);
		} catch (Exception e) {
			e.printStackTrace(System.out);
			System.exit(1);
		}
	}

	private static Properties loadProperties() throws IOException {
		Properties p = new Properties();
		InputStream in = null;
		try {
			in = InsertData.class.getResourceAsStream("/versant.properties");
			if (in == null) {
				throw new IOException("versant.properties not on classpath");
			}
			p.load(in);
		} finally {
			if (in != null)
				in.close();
		}
		return p;
	}

	private PersistenceManager pm;

	public InsertData(PersistenceManager pm) {
		this.pm = pm;
	}

	public void go() throws Exception {

		// Villes
		Ville paris, nice, marseille;
		paris = new Ville("Paris", "Gare de Paris");
		nice = new Ville("Nice", "Gare de Nice");
		marseille = new Ville("Marseille", "Gare de Marseille");

		FacadeInitialisation facade = new FacadeInitialisation(false);
		facade.villeCreate(paris);
		facade.villeCreate(nice);
		facade.villeCreate(marseille);

		// Trains
		Train train1, train2, train3, train4;
		train1 = new Train("Train1", "Modele1", 100);
		train2 = new Train("Train2", "Modele2", 150);
		train3 = new Train("Train3", "Modele3", 200);
		train4 = new Train("Train4", "Modele4", 170);

		ArrayList<String> placesT1 = new ArrayList<String>();
		ArrayList<String> placesT2 = new ArrayList<String>();
		ArrayList<String> placesT3 = new ArrayList<String>();
		ArrayList<String> placesT4 = new ArrayList<String>();
		for (int i = 1; i <= 10; i++) {
			placesT1.add("A" + i);
			placesT1.add("B" + i);
			placesT2.add("A" + i);
			placesT2.add("B" + i);
			placesT2.add("C" + i);
			placesT3.add("A" + i);
			placesT3.add("B" + i);
			placesT3.add("C" + i);
			placesT4.add("D" + i);
			placesT4.add("A" + i);
			placesT4.add("B" + i);
			placesT4.add("C" + i);
			placesT4.add("D" + i);
			placesT4.add("E" + i);
		}
		train1.setPlaces(placesT1);
		train2.setPlaces(placesT2);
		train3.setPlaces(placesT3);
		train4.setPlaces(placesT4);

		facade.trainCreate(train1);
		facade.trainCreate(train2);
		facade.trainCreate(train3);
		facade.trainCreate(train4);

		// Trajets
		Calendar c = Calendar.getInstance();

		c.add(Calendar.DAY_OF_MONTH, -2);
		for (int i = 0; i < 12; i++) {
			c.add(Calendar.DAY_OF_MONTH, 1);

			Date d = c.getTime();
			Trajet t1 = new Trajet("monTrajet", d, 800, paris, nice, train1);
			Trajet t2 = new Trajet("monTrajet", d, 300, nice, marseille, train2);
			Trajet t3 = new Trajet("monTrajet", d, 600, paris, marseille,
					train3);
			Trajet t4 = new Trajet("monTrajet", d, 600, marseille, paris,
					train4);

			facade.trajetCreate(t1);
			facade.trajetCreate(t2);
			facade.trajetCreate(t3);
			facade.trajetCreate(t4);

		}

		// Clients
		Client c1 = new Client("Pierre");
		Client c2 = new Client("Paul");

		facade.clientCreate(c1);
		facade.clientCreate(c2);

		System.out.println("done");

	}

}
